package com.xinqi.modules.exam.student.manager.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.xinqi.common.base.error.CommonErrors;
import com.xinqi.common.mybatis.data.service.impl.BaseServiceImpl;
import com.xinqi.modules.exam.student.domain.CourseExamPaperErrorSetEntity;
import com.xinqi.modules.exam.student.dto.req.CourseExamPaperErrorSetQueryDTO;
import com.xinqi.modules.exam.student.dto.rsp.CourseExamPaperErrorSetResultDTO;
import com.xinqi.modules.exam.student.dto.rsp.CourseExamPaperErrorSetStatsDTO;
import com.xinqi.modules.exam.student.enums.PaperErrorSetTypeEnum;
import com.xinqi.modules.exam.student.manager.CourseExamPaperErrorSetManager;
import com.xinqi.modules.exam.student.mapper.CourseExamPaperErrorSetMapper;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;
import java.util.function.Consumer;

/**
 * 课程测评学生错题集管理层实现
 *
 * @author: linkai
 * @date: 2023/07/02
 */
@Service
@RequiredArgsConstructor
public class CourseExamPaperErrorSetManagerImpl extends BaseServiceImpl<CourseExamPaperErrorSetMapper, CourseExamPaperErrorSetEntity> implements CourseExamPaperErrorSetManager {

    /**
     * 自定义分页查询
     */
    @Override
    public Page<CourseExamPaperErrorSetResultDTO> findPage(Page<Object> page, CourseExamPaperErrorSetQueryDTO query) {
        return this.baseMapper.findPage(page, query);
    }

    /**
     * 自定义列表查询
     */
    @Override
    public List<CourseExamPaperErrorSetResultDTO> findList(CourseExamPaperErrorSetQueryDTO query) {
        return this.baseMapper.findList(query);
    }

    /**
     * 根据id查询 课程测评学生错题集数据
     */
    @Override
    public CourseExamPaperErrorSetEntity findById(Long id) {
        CommonErrors.BAD_REQUEST.check(Objects.nonNull(id), "ID不能为空");
        return getById(id);
    }

    /**
     * 根据id更新 课程测评学生错题集数据，重写
     */
    @Override
    public boolean updateById(CourseExamPaperErrorSetEntity entity) {
        return SqlHelper.retBool(getBaseMapper().updateById(entity));
    }

    /**
     * 根据id删除 课程测评学生错题集数据
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteById(Long id) {
        CommonErrors.BAD_REQUEST.check(Objects.nonNull(id), "ID不能为空");
        return this.removeById(id);
    }

    @Override
    public boolean isExist(Long studentId, PaperErrorSetTypeEnum type, Long questionId) {
        LambdaQueryWrapper<CourseExamPaperErrorSetEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CourseExamPaperErrorSetEntity::getStudentId, studentId)
            .eq(CourseExamPaperErrorSetEntity::getType, type.getCode())
            .eq(CourseExamPaperErrorSetEntity::getQuestionId, questionId);
        return count(wrapper) > 0;
    }

    @Override
    public List<CourseExamPaperErrorSetEntity> findByExamIdAndStudentId(Long examId, Long studentId) {
        LambdaQueryWrapper<CourseExamPaperErrorSetEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.select(
            CourseExamPaperErrorSetEntity::getId,
            CourseExamPaperErrorSetEntity::getQuestionId,
            CourseExamPaperErrorSetEntity::getType
        );
        wrapper.eq(CourseExamPaperErrorSetEntity::getStudentId, studentId)
            .eq(CourseExamPaperErrorSetEntity::getExamPaperId, examId);
        return list(wrapper);
    }

    @Override
    public CourseExamPaperErrorSetStatsDTO statsData(CourseExamPaperErrorSetQueryDTO dto) {
        return this.baseMapper.statsData(dto);
    }

    /**
     * 根据试题id和错题类型查询错题集id ( questionId, id )
     */
    @Override
    public void findIdByQuestionIdsAndType(
        Long studentId,
        List<Long> questionIdList,
        PaperErrorSetTypeEnum tempType,
        Consumer<CourseExamPaperErrorSetEntity> s
    ) {
        if (CollectionUtils.isEmpty(questionIdList)) {
            return;
        }
        LambdaQueryWrapper<CourseExamPaperErrorSetEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.select(
            CourseExamPaperErrorSetEntity::getId,
            CourseExamPaperErrorSetEntity::getQuestionId
        );
        wrapper.in(CourseExamPaperErrorSetEntity::getQuestionId, questionIdList)
            .eq(CourseExamPaperErrorSetEntity::getType, tempType.getCode());
        if (Objects.nonNull(studentId)) {
            wrapper.eq(CourseExamPaperErrorSetEntity::getStudentId, studentId);
        }
        list(wrapper).forEach(s);
    }

}
